package com.hxj.admin.szjy.web;

import com.hxj.admin.szjy.pojo.command.ProjectSaveToNextCommand;
import com.hxj.admin.szjy.pojo.command.StartNewProjectCommand;
import com.hxj.admin.szjy.pojo.query.ProjectQuery;
import com.hxj.admin.szjy.service.ProjectService;
import com.hxj.common.core.pojo.bo.RestResult;
import com.hxj.flowable.domain.dto.FlowTaskDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @Author hxj
 * @Date 2024/10/26 15:36
 * @Description
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/project")
public class ProjectController {

    private final ProjectService projectService;

    @ResponseBody
    @GetMapping("/startNew")
    public RestResult startNewProject(StartNewProjectCommand command) {
        return projectService.startNewProject(command);
    }

    @ResponseBody
    @GetMapping("/saveToNext")
    public RestResult saveToNext(ProjectSaveToNextCommand command) {
        return projectService.completeToNext(command);
    }

    @ResponseBody
    @GetMapping("/returnToLast")
    public RestResult returnToLast(ProjectSaveToNextCommand command) {
        try {
            return projectService.returnToLast(command);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @GetMapping("/queryBaseInfo")
    public RestResult<FlowTaskDto> queryBaseInfo(ProjectQuery query) {
        Optional<FlowTaskDto> projectBaseInfo = projectService.getProjectBaseInfo(query);
        return projectBaseInfo.map(RestResult::success).orElseGet(() -> RestResult.fail("查无数据！"));
    }

}
